import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from "@/views/Index";
import IndexShow from "@/components/Index/IndexShow";
import {getPersonMsg} from "@/api/api";
import Play from "@/components/Play/Play";
import Turntable from "@/components/Play/Turntable";
import Nine from "@/components/Play/Nine";
import AccountSign from "@/components/Sign/AccountSign";
import Robot from "@/components/Play/Robot";
import Book from "@/components/Play/Book";
// import Article from "@/components/Play/Article";
import LittleGame from "@/components/Play/LittleGame";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/animation'
    },
    {
        path: '/index',
        redirect: '/index/indexshow'
    },
    {
        path: '/index',
        component: Index,
        children: [
            {
                path: 'indexshow',
                component: IndexShow,
            },
            {
                path: 'classify',
                component: () => import('../views/Classify.vue')
            },
            {
                path: 'shopcar',
                redirect: 'shopcar/index',
                component: () => import('../views/ShopCar.vue'),
                children: [
                    {
                        path: 'index',
                        component: () => import('../components/ShopCar/ShopCarIndex.vue'),
                    },
                    {
                        path: 'makesure',
                        name: 'makesure',
                        component: () => import('../components/ShopCar/MakeSureOrder.vue'),
                        meta: {commodity: true},
                    }
                ]
            },
            {
                path: 'center',
                component: () => import('../views/Center.vue'),

            },
        ]
    },
    {
        path: '/goods',
        component: () => import('../views/GoodDetail.vue'),
        // component: GoodDetail,
        // meta: {commodity: true},
    },

    {
        path: '/search',
        component: () => import('../views/Search.vue')
    },
    {
        path: '/onlinePay',
        component: () => import('../views/OnlinePay.vue'),
        meta: {commodity: true},
    },
    //  账号密码登录
    {
        path: '/accountsign',
        component: AccountSign,
    },
    //  手机号登录
    {
        path: '/phonesign',
        component: () => import('../components/Sign/PhoneSign.vue')
    },
    //  注册
    {
        path: '/register',
        component: () => import('../components/Sign/Register.vue')
    },
    //  忘记密码
    {
        path: '/forgotpassword',
        component: () => import('../components/Sign/Forgotpassword.vue')
    },
    //  个人信息页
    {
        path: '/personal',
        component: () => import('../components/Sign/PersonalInformationPage.vue'),
        meta: {needAlign: true},
    },
    //修改个人信息页
    {
        path: '/information',
        component: () => import('../components/Sign/Information.vue'),
        meta: {needAlign: true},
    },
    //开场动画
    {
        path: '/animation',
        component: () => import('../components/Animation.vue'),
        // meta: {needAlign: true},
    },
    {
        path: '/mineAddress',
        component: () => import('../views/AddressList.vue')
    },
    //第三方接口搞笑段子
    {
        path: '/play',
        component: Play,
    },
    {
      path: '/air',
      component: () => import('../views/AirPage.vue')
  },
  {
    path: '/mineAddress',
    component: () => import('../views/AddressList.vue')
},
    //搞笑段子
    {
        path:'/play',
        component: Play,
    },
    {
        path: '/turntable',
        component: Turntable
    },
    {
        path: '/nine',
        component: Nine
    },
    // 第三方接口文字转图片
    {
        path: '/qrcode',
        component: () => import('../components/Play/QRcode.vue')
    },
    // 第三方接口每日60秒读报
    {
        path: '/dailynews',
        component: () => import('../components/Play/DailyNews.vue')
    },
    // 第三方接口每日60秒读报
    {
        path: '/watercress',
        component: () => import('../components/Play/Watercress.vue')
    },
    // {
    //     path:'/redenvelopes',
    //     component: () => import('../components/Sign/RedEnvelopes')
    // },
    // },
    {
        path: '/robot',
        component: Robot
    },
    {
        path: '/book',
        component: Book
    },
    {
        path: '/2048',
        component: LittleGame
    },
    {
        path: '/shortVideo',
        component: () => import('../components/Video/Video.vue')
    },
    {
        path: '/mineAllOrders',
        component: () => import('../views/MineAllOrders.vue')
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {

    //利用地址控制是否跳转
    // console.log(to.meta.needAlign)

    // next('/')
    //needAlign 默认为true 不允许没有个人信息，进入相关页面

    if (to.meta.commodity) {
        // Toast('请先登录')
        // console.log(to.path)

        //请求个人信息判断是否登录
        getPersonMsg().then(res => {
            if (res.data.code == 0) {
                next()
            } else {
                next('/accountsign')
            }
        })
    } else if (to.meta.needAlign) {
        //请求个人信息判断是否登录
        getPersonMsg().then(res => {
            if (res.data.code == 0) {
                next()
            } else {
                next('/')
            }
        })
    } else {
        next()
    }
})

export default router
